﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;

using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Config;
using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Events;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public class Fav5DTO
    {
        public int Slot { get; set; }
        public string ContactID { get; set; }
        public string DisplayName { get; set; }
        public ClickAction DefaultClickAction { get; set; }
        public string DefaultClickActionFriendlyName { get; set; }
        public bool IsPhoneNumber { get; set; }

        public Fav5DTO(int slot, string contactID, string displayName, ClickAction defaultClickAction, bool isPhoneNumber)
        {
            this.Slot = slot;
            this.ContactID = contactID;
            this.DisplayName = displayName;
            this.DefaultClickAction = defaultClickAction;
            this.DefaultClickActionFriendlyName = Microsoft.Samples.Office.UnifiedCommunications.Gadget.DefaultClickAction.EnumToFriendlyName(defaultClickAction);
            this.IsPhoneNumber = isPhoneNumber;
        }
    }

    public class Fav5ObservableCollection : ObservableCollection<Fav5DTO>
    {
        public Fav5ObservableCollection()
        {
            FavoriteElementCollection fav5 = GadgetConfigurationManager.GetCurrentFav5List();

            // There at most 5 elements in the collection.
            //  A call to GetByOrderKey will return null if no element exists for that order key.
            //  Attempt to get all elements, always gives us a collection of 5 items.
            //  Just re-instantiate this every time we need the latest Fav5 collection.

            for (int i = 1; i <= 5; i++)
            {
                FavoriteElement fav = fav5.GetByOrderKey(i);

                Fav5DTO fav5DTO;

                if (fav == null)
                    fav5DTO = new Fav5DTO(i, default(string), default(string), ClickAction.None, false);
                else
                    fav5DTO = new Fav5DTO(i, fav.ContactID, fav.DisplayName,
                        (ClickAction)Enum.Parse(typeof(ClickAction), fav.DefaultClickAction), fav.IsPhoneNumber);

                this.Add(fav5DTO);
            }
        }
    }
}
